package com.codinghero.example.cli;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;

import com.codinghero.util.DateUtils;

public class CliUnExample {
	public static void main(String[] args) {
		CommandLine cmd = parseArgs(args);
		String countdate = null;
		if(cmd.hasOption("d")) {
			countdate = cmd.getOptionValue("d");
		}
		else {
			countdate = DateUtils.getCurDateStr("yyyyMMdd");
		}
		System.out.println(countdate);
	}
	
	private static CommandLine parseArgs(String[] args) {
		Options options = new Options();
		@SuppressWarnings("static-access")
		Option countdateOpt = OptionBuilder.withArgName("countdate").hasArg()
				.withDescription("the date of the data").create("d");
		options.addOption(countdateOpt);
		
		CommandLineParser parser = new PosixParser();
		try {
			return parser.parse(options, args);
		} catch (ParseException e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}
}
